/**
 * ...
 * @author Thot
 */

function LayersMediator( p_view )
{
	this.init( LayersMediator.NAME, p_view );
}

POO.extend( LayersMediator, Mediator );


	// static
	LayersMediator.NAME = "LayersMediator";
	
	
	//private
	LayersMediator.prototype._screen = null;
	
	// public
	LayersMediator.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [p_name, p_data] );
		this._screen = new LayersScreen();
		this.listenNotifications();	
		this.initLayersList();
	};
	
	LayersMediator.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.ADD_LAYER, 			this.onNotification );
		this.addNotificationListener( ApplicationNotification.SELECT_LAYER, 		this.onNotification );
		this.addNotificationListener( ApplicationNotification.REMOVE_LAYER, 		this.onNotification );
		this.addNotificationListener( ApplicationNotification.CLEAN_LAYERS, 		this.onNotification );
		this.addNotificationListener( ApplicationNotification.DISPLAY_MAP_LAYERS, 	this.onNotification );
	};

	LayersMediator.prototype.onNotification = function( p_notificationType, p_data )
	{
		var proxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var mapProxy = this.getFacade().getProxy( MapProxy.NAME );
		
		switch ( p_notificationType )
		{
				
			case ApplicationNotification.ADD_LAYER:
				this._screen.addLayer( p_data );
				break;
				
			case ApplicationNotification.SELECT_LAYER:
				proxy.currentLayer = p_data;
				break;

			case ApplicationNotification.REMOVE_LAYER:
				this._screen.removeLayer( proxy.currentLayer );
				break;
				
			case ApplicationNotification.CLEAN_LAYERS:
				this._screen.clean();
				break;
				
			case ApplicationNotification.DISPLAY_MAP_LAYERS:
				
				var vo = mapProxy.getMapVOById(proxy.currentMap);
				var layerVO = null;
				var i = 0;
				var max = vo.layers.length;
				
				for( i = 0; i < max ; i++ )
				{
					layerVO = vo.layers[i];
					this._screen.addLayer( layerVO.name );
				}
				
				break;
		}
	};
	
	LayersMediator.prototype.initLayersList = function()
	{
		var container = this.getViewComponent();
		var list = document.getElementById("layersList");
		var addButton = document.getElementById("addLayerButton");
		var removeButton = document.getElementById("removeLayerButton");
		this._screen.init( list, addButton, removeButton );
		this._screen.signal.addListener( LayersScreen.ON_SELECT, this.selectLayerHandler, this );
		this._screen.signal.addListener( LayersScreen.ADD_LAYER, this.addLayerHandler, this );
		this._screen.signal.addListener( LayersScreen.REMOVE_LAYER, this.removeLayerHandler, this );
	};
	
	LayersMediator.prototype.selectLayerHandler = function(p_layerId)
	{
		this.getFacade().sendNotification( ApplicationNotification.SELECT_LAYER, p_layerId );
	};
	
	LayersMediator.prototype.addLayerHandler = function(p_layerId)
	{
		this.getFacade().sendNotification( ApplicationNotification.ADD_LAYER, p_layerId );
	};
	
	LayersMediator.prototype.removeLayerHandler = function(p_layerId)
	{
		this.getFacade().sendNotification( ApplicationNotification.REMOVE_LAYER, p_layerId );
	};
